# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
import csv
import os
from pymongo import MongoClient

from utils.HttpUtils import HttpUtils

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
沙石帮APP
"""


class TestRequest():
    def __init__(self):
        # 声明一个CookieJar对象实例来保存cookie
        # requests.utils.dict_from_cookiejar(html.cookies)  # 获取cookies转dict
        # res.cookies.get_dict()  # 获取cookies
        self.cookie = cookiejar.CookieJar()
        # self.ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        # self.ua = UserAgent(verify_ssl=False)  #
        self.headers = {
            'Authorization':'',
            'Content-Type': 'application/json',
            'Host': 'app.shashibang.site',
            'Connection': 'Keep-Alive',
            'Accept-Encoding': 'gzip',
            'User-Agent': 'Mozilla/5.0 (Linux; Android 9; SM-N9700 Build/PQ3B.190801.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Safari/537.36 uni-app Html5Plus/1.0 (Immersed/24.0)'
        }

    def get_contents(self, cursor, category):
        postData = {
            'searchValue': "",
            'pageNumber': cursor,
            'pageSize': 10,
            'category': category,  # 车队信息：10 货源信息：11 沙源企业:12 石源企业:13 收料单位:14
            'province': "",  # 省份  河北省
            'city': "",  # 城市  石家庄市
            'enterType': "",
            'sortType': "",
            'type': 1,
            'companyType': "",
            'ifSell': ""
        }
        url = f"https://app.shashibang.site/app/message/public/getModuleMessage"
        html = HttpUtils.do_request("POST", url, self.headers, json.dumps(postData))

        data_json = json.loads(html.text)

        pageCount = data_json['data']['pages']  # 总页数
        pageNum = data_json['data']['pageNum']  # 当前页号
        print(f"=========第【{pageNum}】页========共【{pageCount}】页=====")

        for d in data_json['data']['list']:
            dict_data = dict()
            dict_data['类别'] = d['categoryText']
            dict_data['车队车型'] = d['companyType']
            dict_data['车型数量'] = d['amount']
            dict_data['发布时间'] = d['createTime']
            dict_data['省份'] = d['province']
            dict_data['城市'] = d['city']
            dict_data['地址'] = d['address']
            dict_data['坐标'] = d['location']
            dict_data['公司名称'] = d['companyName']
            dict_data['联系人'] = d['contactName']
            dict_data['联系电话'] = d['contactPhone']
            dict_data['内容'] = d['content']
            # 保存数据
            HttpUtils.dict_to_csv("沙石帮", dict_data)

        if pageNum < pageCount:
            time.sleep(2)
            self.get_contents(pageNum + 1, category)

    def get_type(self):
        """
        获取类别ID
        :return:
        """
        url = f"https://app.shashibang.site/app/index/public/getSysModule"
        html = HttpUtils.do_request("POST", url, self.headers, '')

        data_json = json.loads(html.text)

        for t in data_json['data']:
            dict_type = dict()
            dict_type['ID'] = t['id']  # 对应内容postData中的category
            dict_type['分类名称'] = t['name']
            print(dict_type)


if __name__ == '__main__':

    test = TestRequest()

    # 获取类别
    # test.get_type()
    # 获取内容 # 车队信息：10 货源信息：11 沙源企业:12 石源企业:13 收料单位:14
    test.get_contents(1, 11)
